<?php
/**
 * Create By: PhpStorm
 * User: niechenguang
 * File: Circle.php
 * Date: 2017/1/2
 * Time: 23:13
 */
namespace app\api\model\dao;

use think\Model;
use think\Db;

use app\library\Flag;

class IntegralLog extends Model
{
    protected $table = 'integral_log';

    protected $pk = 'id';

    public function add($arrInput)
    {
        unset($arrInput[$this->pk]);

        $this->data($arrInput);
        $this->save();
        return $this->data[$this->pk];
    }

    public function getByUseridAndSourceAndDate($userid, $source = '', $date = 0)
    {

        $arrCond = [
            'userid' => ['=', $userid],
            'source' => ['=', $source],
        ];

        if (empty($date)) {
            $arrCond['date'] = ['=', date('Ymd')];
        }

        $ret = Db::table($this->table)->where($arrCond)->select();

        return $ret;
    }

    public function getCountByUseridAndSourceAndDate($userid, $source = '', $date = 0)
    {

        $arrCond = [
            'userid' => ['=', $userid],
            'source' => ['=', $source],
        ];

        if (!empty($date)) {
            $arrCond['date'] = ['=', $date];
        }

        $ret = Db::table($this->table)->where($arrCond)->count();
        if (empty($ret)) {
            $ret = 0;
        }

        return $ret;
    }


}
